<?php
class Manage_document_model extends CI_model {

    public function __construct(){
        $this->load->database();
    }

    public function display_user_account($limit, $start, $user)
    {

        $this->db->select ('*');
        $this->db->from   ('user_col');
        $this->db->join   ('user_accounts', 'user_accounts.coll_id = user_col.coll_id', 'left');
        $this->db->join   ('user_profile', 'user_profile.user_id = user_accounts.user_id', 'left');
        $this->db->order_by('coll_name', 'asc');
        $this->db->limit  ($limit, $start);
        $this->db->where  ('user_col.coll_id !=', 0);
        $this->db->where  ('user_accounts.type_no', 1);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function announcement_to_all($limit, $start, $user)
    {

        $this->db->select ('*, announcement.coll_id');
        $this->db->from   ('announcement');
        $this->db->join   ('user_accounts', 'user_accounts.user_id = announcement.user_id', 'left');
        $this->db->join   ('user_profile', 'user_profile.user_id = user_accounts.user_id', 'left');
        $this->db->join   ('user_col', 'user_col.coll_id = announcement.coll_id', 'left');
        $this->db->join   ('user_type', 'user_type.type_no = user_accounts.type_no', 'left');
        $this->db->order_by('coll_name', 'asc');
        $this->db->limit  ($limit, $start);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function announcement_by_college($limit, $start, $user)
    {

        //The user_accounts is maintained because it is needed.
        $this->db->select ('*, announcement.coll_id');
        $this->db->from   ('announcement');
        $this->db->join   ('user_accounts', 'user_accounts.user_id = announcement.user_id', 'left');
        $this->db->join   ('user_profile', 'user_profile.user_id = user_accounts.user_id', 'left');
        $this->db->join   ('user_col', 'user_col.coll_id = announcement.coll_id', 'left');
        $this->db->join   ('user_type', 'user_type.type_no = user_accounts.type_no', 'left');
        $this->db->order_by('coll_name', 'asc');
        $this->db->limit  ($limit, $start);
        $this->db->where  ('user_col.coll_id !=', 0);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function display_user($limit, $start, $user)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->join   ('user_col', 'user_accounts.coll_id = user_col.coll_id');
        $this->db->order_by('coll_name', 'asc');
        $this->db->limit  ($limit, $start);
        $this->db->where  ('user_col.coll_id !=', 0);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function display_user_profile()
    {

        $this->db->select ('*');
        $this->db->from   ('user_profile');

        $query = $this->db->get();

        return $query->result();

    }

    public function display_user_college()
    {

        $this->db->select ('*');
        $this->db->from   ('user_col');
        $this->db->where   ('coll_id !=', 0);

        $query = $this->db->get();

        return $query->result();

    }

    public function display_user_type()
    {

        $this->db->select ('*');
        $this->db->from   ('user_type');
        $this->db->where  ('name_type !=','Administrator');
        $this->db->where  ('name_type !=','Uncategorized');
        $this->db->where  ('name_type !=','Dean');

        $query = $this->db->get();

        return $query->result();

    }

    public function check_acc($username)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->where  ('user_id', $username);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function add_announcement($input1){

        $this->db->insert ('announcement',$input1);

    }

    public function insert_dept($input1){

        $this->db->insert ('user_col',$input1);

    }

    public function edit_college($input1,$id){


        $this->db->where    ('coll_id',$id);
        $this->db->update   ('user_col',$input1);


    }

    public function get_dept_id($input1){

        $this->db->select   ('coll_id');
        $this->db->from     ('user_col');
        $this->db->where    ('coll_name', $input1);

        $query = $this->db->get();

        return $query->result_array();

    }

    public function delete($data){

        $this->db->delete('announcement', array('annc_id' => $data));

    }

    public function get_user_count(){

        return $this->db->count_all("user_accounts") - 1;

    }

    public function check_user($user)
    {

        $this->db->select ('*');
        $this->db->from   ('user_accounts');
        $this->db->join   ('user_type', 'user_type.type_no = user_accounts.type_no', 'left');
        $this->db->where  ('user_id', $user);

        $query = $this->db->get();

        return $query->row_array();

    }

}